
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../layui/css/public.css" media="all"/>

</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>

<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-inline">
                <input type="text" name="title" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="newsTable" lay-filter="newsTable"></table>
<div style="display: none;" id="newsToolBar">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="add">增加</button>
    <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="deleteBatch">批量删除</button>
</div>
<div id="newsBar" style="display: none;">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="del">删除</a>

</div>

<div id="viewNewsDiv" style="padding: 10px;display: none;">
    <h2 id="view_title" align="center"></h2>
    <hr>
    <div style="text-align: right;">
        发布人:<span id="view_opername"></span>  <span style="display: inline-block;width: 20px" ></span>
        发布时间:<span id="view_createtime"></span>
    </div>
    <hr>
    <div id="view_content"></div>
</div>
<script src="../layui/layui.js"></script>
<script type="text/javascript">
    var tableIns;
    layui.use('table', function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var layedit = layui.layedit;
        var laydate = layui.laydate;
        var $ = layui.jquery;

        tableIns = table.render({
            elem: '#newsTable'
            ,url:'news/getAll'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
            ,toolbar: '#newsToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '公告'
            , cols: [[   //列表数据
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID',align:'center'}
                ,{field:'title', title:'公告标题',align:'center'}
                ,{field:'content', title:'公告内容',align:'center'}
                ,{field:'createtime', title:'发布时间',align:'center'}
                ,{field:'opername', title:'发布人',align:'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#newsBar', width:220,align:'center'}
            ]]
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                ,limit:3
                ,limits:[1,3,5]
                ,groups: 3 //只显示 1 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页

            }
        });
        //模糊查询
        $("#doSearch").click(function () {
            var params = $("#searchFrm").serialize();
            tableIns.reload({
                url: "news/getAll?" + params,
                page:{curr:1}
            })
        });
        //初始化查询时间
        //日期时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
        });
        table.on("toolbar(newsTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openaddNews();
                    break;
                case 'deleteBatch':
                    deleteBatch();
                    break;
            }
            ;
        })
        //监听行工具事件
        table.on('tool(newsTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除【' + data.title + '】这个公告吗', function (index) {
                    obj.del();
                    layer.close(index);
                    $.post("news/delNews", {id: data.id}, function (backData) {
                        if("success" == backData){
                            table.reload("newsTable",{});
                        }
                    })
                });
            } else if (layEvent === 'edit') { //编辑
                openupdateNews(data);
            }else if(layEvent==='viewNews'){
                viewNews(data);
            }
        });



        //保存
        form.on("submit(doSubmit)", function (obj) {
            layedit.sync(editIndex);//把富文本里面的数据同步到自己写的textarea里面
            //序列化表单数据
            var params = $("#dataFrm").serialize();
            alert(params)
            $.post(url, params, function (obj) {
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex)
                //刷新数据 表格
                tableIns.reload();
            })
        });

        //批量删除
        function deleteBatch() {
            //得到选中的数据行
            var checkStatus = table.checkStatus('newsTable');
            var data = checkStatus.data;
            var ids = '';
            //遍历data
            for (var i = 0; i < data.length; i++){
                ids+="&ids="+data[i].id;
            }
            //alert(ids);
            //调用ajax
            layer.confirm('真的删除这些公告吗？',function(index) {

                $.get("news/deleteBatchNews", ids,
                    function (backData) {
                        //alert(backData);
                        if ("success" == backData) {
                            layer.close(index, function () {
                                table.reload("newsTable", {});//刷新当前页面
                            });
                        }
                    })
            });
        }
    });
</script>
<script>
    function openaddNews() {
        layer.open({
            type: 1,
            title: '添加公告',
            content: $("#updateUser").html(),
            area: ['800px', '550px'],
            btn: ['提交', '取消'],
            success: function () {
                layui.form.render();
            },
            yes: function (index,layero) {
                var table = layui.table;
                $.ajax({
                    type: "POST",
                    url: "news/addNews",
                    data: $("#updateUserForm").serialize(),
                    success: function (msg) {
                        layer.close(index, function(){
                            table.reload("newsTable",{});//刷新当前页面
                        });
                    }
                });
            },
            btn2: function () {
                alert("已取消")
            }
        });
    }
    $("#dataFrmResetBtn").click(function(){
        layedit.setContent(editIndex,"");
    });
    //打开修改页面
    function openupdateNews(data) {
        layer.open({
            type: 1,
            title: '修改公告',
            content: $("#updateUser").html(),
            area: ['800px', '550px'],
            btn: ['提交', '取消'],
            success: function () {
                $("#id").val(data.id);
                $("#title").val(data.title);
                $("#content").val(data.content);
                $("#opername").val(data.opername);

                $("#createtime").val(layui.util.toDateString(data.createtime,'yyyy-MM-dd'));

                layui.form.render();
            },
            yes: function (index,layero) {
                var table = layui.table;
                $.ajax({
                    type: "POST",
                    url: "news/updateNews",
                    data: $("#updateUserForm").serialize(),
                    success: function (msg) {

                        layer.close(index, function(){
                            table.reload("newsTable",{});//刷新当前页面
                        });
                    }
                });
            },
            btn2: function () {
                alert("已取消")
            }
        });
    }
</script>
<script type="text/html" id="updateUser">
    <form id="updateUserForm" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-block">
                <input type="hidden" name="id" id="id">
                <input type="text" id="title" name="title"  placeholder="请输入公告标题" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-block">
                <input type="text" class="layui-inputtextarea "  name="content" lay-verify="content" id="content">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发布时间:</label>
            <div class="layui-input-block">
                <input type="date" name="createtime" id="createtime" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布人:</label>
            <div class="layui-input-block">
                <input type="text" name="opername" id="opername" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>

</script>
</body>
</html>


